package tutor.mina.client;

import java.net.InetSocketAddress;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.common.IoConnector;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

public class IoHandlerStyleTimeClient {

    static Log log = LogFactory.getLog( IoHandlerStyleTimeClient.class );

    public static void main( final String[] args ) {
        final IoConnector connector = new NioSocketConnector();

        connector.setHandler( new IoHandlerAdapter() {

            @Override
            public void messageReceived( final IoSession session,
                    final Object message )
                throws Exception {
                log.info( message );
            }

        } );

        connector.getFilterChain().addLast( "codec",
                new ProtocolCodecFilter( new TextLineCodecFactory() ) );

        connector.connect( new InetSocketAddress( 8150 ) )
                .awaitUninterruptibly();

        connector.dispose();
    }

}
